package cc.mrbird.assets.utils;

import cc.mrbird.assets.domain.*;
import cc.mrbird.assets.service.*;
import cc.mrbird.system.domain.User;
import cc.mrbird.unit.domain.AuxiliaryInfor;
import cc.mrbird.unit.service.AuxiliaryInforService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * 文件上传工具包
 */
@Component
@RestController
public  class FileUtil {

    @Autowired
    private AssetManaService assetManaService;
    @Autowired
    private  ParkService parkService;
    @Autowired
    private TowerService towerService;
    @Autowired
    private ElementService elementService;
    @Autowired
    private LayerService layerService;
    /**
     * @param file
     * @return
     */
    public  boolean add(MultipartFile file,AssetMana assetMana,User user) {

        boolean result = false;
        //for (MultipartFile file : files) {
            String path = System.getProperty("user.dir"); //获取当前项目根目录
            path += File.separator + "src" + File.separator + "main" + File.separator + "resources" + File.separator
                    + "static" + File.separator + "img" + File.separator + "assets";
            String fileName = file.getOriginalFilename();
            // 生成新的文件名
            String realPath = path + File.separator + FileNameUtils.getFileName(fileName);
            /*  //使用原文件名
             String realPath = path + "/" + fileName;*/
            File dest = new File(realPath);
            //判断文件父目录是否存在
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdir();
            }
            if(dest.exists()){
                dest.mkdir();
            }
            try {
                //保存文件

                file.transferTo(dest);
                assetMana.setInformation_plan(realPath);
                result = true;
            } catch (Exception e) {
                e.printStackTrace();
                result = false;
            }
       // }
        if(result==true){
            String park = assetMana.getPark();
            Park park1 = parkService.findByName(park);
            if(park1==null){
                Park park2 = new Park();
                park2.setPark_name(park);
                parkService.add(park2);
            }
            String tower = assetMana.getTower();
            Tower tower1 = towerService.findByName(tower);
            if(tower1==null){
                Tower tower2 = new Tower();
                tower2.setTower_name(tower);
                towerService.add(tower2);
            }
            String element = assetMana.getElement();
            Element element1 = elementService.findByName(element);
            if(element1==null){
                Element element2 = new Element();
                element2.setElement_name(element);
                elementService.add(element2);
            }
            String layer = assetMana.getLayer();
            Layer layer1 = layerService.findByName(layer);
            if(layer1==null){
                Layer layer2 = new Layer();
                layer2.setLayer_name(layer);
                layerService.add(layer2);
            }
            String home = assetMana.getHome();
            String deployment_location = park+tower+element+layer+home;
            assetMana.setDeployment_location(deployment_location);
            assetManaService.add(assetMana,user);
        }
        return result ;
    }

    public  boolean update(MultipartFile file,AssetMana assetMana) {

        boolean result = false;
        //for (MultipartFile file : files) {
        String path = System.getProperty("user.dir"); //获取当前项目根目录
        path += File.separator + "src" + File.separator + "main" + File.separator + "resources" + File.separator
                + "static" + File.separator + "img" + File.separator + "assets";
        String fileName = file.getOriginalFilename();
        // 生成新的文件名
        String realPath = path + File.separator + FileNameUtils.getFileName(fileName);
            /*  //使用原文件名
             String realPath = path + "/" + fileName;*/
        File dest = new File(realPath);
        //判断文件父目录是否存在
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdir();
        }
        if(dest.exists()){
            dest.mkdir();
        }
        try {
            //保存文件

            file.transferTo(dest);
            assetMana.setInformation_plan(realPath);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
            result = false;
        }
        // }
        if(result==true){
            String park = assetMana.getPark();
            Park park1 = parkService.findByName(park);
            if(park1==null){
                Park park2 = new Park();
                park2.setPark_name(park);
                parkService.add(park2);
            }
            String tower = assetMana.getTower();
            Tower tower1 = towerService.findByName(tower);
            if(tower1==null){
                Tower tower2 = new Tower();
                tower2.setTower_name(tower);
                towerService.add(tower2);
            }
            String element = assetMana.getElement();
            Element element1 = elementService.findByName(element);
            if(element1==null){
                Element element2 = new Element();
                element2.setElement_name(element);
                elementService.add(element2);
            }
            String layer = assetMana.getLayer();
            Layer layer1 = layerService.findByName(layer);
            if(layer1==null){
                Layer layer2 = new Layer();
                layer2.setLayer_name(layer);
                layerService.add(layer2);
            }
            String home = assetMana.getHome();
            String deployment_location = park+tower+element+layer+home;
            assetMana.setDeployment_location(deployment_location);
            assetManaService.update(assetMana);
        }
        return result ;
    }
}
